package com.example.day03.login

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.day03.entity.CodeEntity
import com.example.day03.entity.ReginLoginEntity
import com.example.day03.entity.Res
import com.example.day03.mvvm.BaseViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody

class ReginViewModel:BaseViewModel() {
    val codesuccess=MutableLiveData<Res<CodeEntity>>()
    val codefail=MutableLiveData<String>()
    fun getCode(phone:String){
        viewModelScope.launch {
            codesuccess.postValue(apiService.getCode(phone))
        }
    }

    val reginsuccess=MutableLiveData<Res<ReginLoginEntity>>()
    val reginfail=MutableLiveData<String>()
    fun getRegin(phone:String,code:String){
        viewModelScope.launch {
            val map=HashMap<String,String>()
            map["phone"]=phone
            map["captcha"]=code
            val json = Gson().toJson(map)
            val body = RequestBody.create("application/json;charset=utf-8".toMediaType(), json)
            reginsuccess.postValue(apiService.getRegin(body))
        }
    }

    val loginsuccess=MutableLiveData<Res<ReginLoginEntity>>()
    val loginfail=MutableLiveData<String>()
    fun getlogin(phone:String,pass:String){
        viewModelScope.launch {
            val map=HashMap<String,String>()
            map["account"]=phone
            map["password"]=pass
            val json = Gson().toJson(map)
            val body = RequestBody.create("application/json;charset=utf-8".toMediaType(), json)
            loginsuccess.postValue(apiService.getLogin(body))
        }
    }
}